local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname)

-- Load support for MT game translation.
local S = minetest.get_translator(modname)

---[ Sponge ]-----
------------------

function sponge_interaction(pos,find,replace,find_is_group)
	local sponge_is_wet = false
	for xi = -1,1 do
		for zi = -1,1 do
			for yi = -1,1 do
				local p = {x=pos.x+xi,y=pos.y+yi,z=pos.z+zi}
				local n = minetest.get_node(p).name
				if n then
					local check
					if not find_is_group then
						check = n == find
					else
						local grp = minetest.get_node_group(n, find)
						check = grp > 0
					end
					if check then 
						minetest.set_node(p, {name=replace})
						sponge_is_wet = true 
					end
				end
			end
		end
	end
	return sponge_is_wet
end

--
-- Fake air used by sponge 
--

--~ minetest.register_node("funny_matter:air", {
	--~ description = "funny_matter fake air",
	--~ tiles = {"funny_matter_sponge.png^[colorize:#FFA500:120"},
	--~ drawtype = "glasslike",
	--~ drawtype = "airlike",
	--~ is_ground_content = true,
--~ --	liquidtype =  "none",
	--~ -- liquid_alternative_flowing = "sand_portals:quick_sand_field",
	--~ -- liquid_alternative_source  = "sand_portals:quick_sand",
	--~ walkable = true,
	--~ pointable = true,
	--~ diggable = true,
	--~ buildable_to = false,
	--~ paramtype = "light",
--~ --	liquid_viscosity = 30 ,
	--~ groups = {not_in_creative_inventory = 1} -- water=3, liquid=3 , 
	--~ drop = "funny_matter:sponge_wet" ,
	--~ on_destruct = function(pos) 
		--~ sponge_interaction(pos,"funny_matter:air","air",false)
		--~ sponge_interaction(pos,"funny_matter:sponge_wet","air",false)
	--~ end,
--~ })

--
-- Sponge
--

minetest.register_node("funny_matter:sponge", {
	description = S("Sponge"),
	tiles = {"funny_matter_sponge.png"},
	groups = {snappy = 3, fall_damage_add_percent = -80, bouncy=-20},
	sounds = default.node_sound_defaults(),
})

minetest.register_node("funny_matter:sponge_wet", {
	description = S("Water filled sponge"),
	tiles = {"funny_matter_sponge.png^[colorize:#FFA500:120"},
	visual_scale = 3,
	groups = {snappy = 3, fall_damage_add_percent = -80, bouncy=-25},
	sounds = default.node_sound_water_defaults(),
	--~ on_destruct = function(pos) 
		--~ sponge_interaction(pos,"funny_matter:air","air",false)
	--~ end,
})

minetest.register_abm({
	label = "Sponge absorbing",
	nodenames = {"funny_matter:sponge"},
	neighbors = {"group:water"},
	interval = 3,
	chance = 1,
	catch_up = false,
	action = function(pos) --node, active_object_count, active_object_count_wider
		local is_wet = sponge_interaction(pos,"water","funny_matter:sponge_wet",true)			
		if is_wet then 
			minetest.set_node(pos, {name="funny_matter:sponge_wet"})
		end
	end,
})

minetest.register_craft({
	output = 'funny_matter:sponge',
	recipe = {
		{'group:leaves','group:leaves','group:leaves'},
		{'group:leaves','group:sand','group:leaves'},
		{'group:leaves','group:leaves','group:leaves'},
		},
})

minetest.register_craft({
	type = "cooking",
	output = "funny_matter:sponge",
	recipe = "funny_matter:sponge_wet",
	cooktime = 1,

})



--
-- Fall damage amortisseur and trampolines
--

-- --[[
minetest.register_node("funny_matter:cushion", {
	description = S("Cushion"),
	tiles = {"funny_matter_cushion.png"},
	drawtype = "nodebox",
	paramtype = "light",
	paramtype2 = "facedir",
	is_ground_content = false,
	node_box = {
		type = "fixed",
		fixed = {-0.5, -0.5, -0.5, 0.5, 0, 0.5},
	},
	groups = {snappy = 3, wool = 3, fall_damage_add_percent = -75, bouncy=-30},
})

minetest.register_node("funny_matter:cushion_block", {
	description = S("Cushion"),
	tiles = {"funny_matter_cushion.png"},
	is_ground_content = false,
	groups = {snappy = 3, wool = 3, fall_damage_add_percent = -85, bouncy=-30},
})

--[[
minetest.register_node("funny_matter:antigrav", {
	description = S("Antigravity block"),
	tiles = {"funny_matter_antigrav.png"},
	groups = {snappy = 3, wool = 3, fall_damage_add_percent = -90},
})
--]]

minetest.register_node("funny_matter:trampoline", {
	description = S("Trampoline"),
	tiles = {"funny_matter_trampoline.png"},
	groups = {snappy = 3, wool = 3, fall_damage_add_percent=-80, bouncy=110},
	sounds = {footstep = {name="xdecor_bouncy", gain=0.8}}
})


minetest.register_craft({
	output = 'funny_matter:cushion 6',
	recipe = {
		{'funny_matter:cushion_block','funny_matter:cushion_block','funny_matter:cushion_block'},
		},
})

minetest.register_craft({
	output = 'funny_matter:cushion',
	recipe = {
		{'group:wool','group:wool','group:wool'},
		{'group:wool','group:leaves','group:wool'},
		},
})

minetest.register_craft({
	output = 'funny_matter:cushion_block',
	recipe = {
		{'group:wool','group:wool','group:wool'},
		{'group:wool','group:leaves','group:wool'},
		{'group:wool','group:wool','group:wool'},
		},
})

minetest.register_craft({
	output = 'funny_matter:cushion_block',
	recipe = {
		{'funny_matter:cushion'},
		{'funny_matter:cushion'},
		},
})

minetest.register_craft({
	output = 'funny_matter:trampoline',
	recipe = {
		{'group:sapling','group:sapling','group:sapling'},
		{'funny_matter:sponge','funny_matter:sponge','funny_matter:sponge',},
		},
})

--]]

